package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
import ModelLayer.*;
import java.util.ArrayList;
import java.util.HashMap;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    
    /**
     * Constructor
     */
    public DvdUI()
    {
        DVDCtr dvdCtr = new DVDCtr();
        start(dvdCtr);
    }
    
    /**
     * Initialises the DVD controller object, and starts the dvd menu
     */
    public void start(DVDCtr ctr)
    {
        dvdCtr= ctr;
        dvdMenu();
    }
    
    /**
     * DVD menu loop
     */
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise =   writeDVDMenu();
            if (choise == 1)
            {
                findDVD();
            }
            else{if(choise == 2){
                   createDVD();
                }
                else{
                    if(choise == 3){
                        createCopy();
                    }
                    else{
                        if(choise == 4){
                             deleteDVD();
                        }
                        else{
                            if(choise == 5){
                                listCurrentDvds();
                            }
                            else {
                                if(choise == 6)
                                {
                                    listAllDvds();
                                }
                                else
                                {
                                    exit = true;
                                }
                            }
                            //end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
            //make sure that written text is not overwrited right away
            pause();
            
        }//end while
    }
    /**
     * Writes the dvd menu options to the screen, and returns the choise
     */
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Create DVD description");
            System.out.println(" (3) Create DVD specimen");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Show all DVDs");
            System.out.println(" (7) Return");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    
    /**
     * Asks for and returns a dvd producer
     */
    private String inputProducer()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD Producer: ");
        String producer = keyboard.nextLine();
        return producer;
    }
    
    /**
     * Asks for and returns a dvd publisher
     */
    private String inputPublisher()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD Publisher: ");
        String publisher = keyboard.nextLine();
        return publisher;
    }
    
    /**
     * Asks for and returns a dvd title
     */
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    
    /**
     * Asks for and returns the date of purchase of a specific DVD
     */
    private String inputDateOfPurchase()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("When was the DVD bought?: " );
        String date = keyboard.nextLine();
        return date;
    }
    
    /**
     * Asks for and returns the serial number of a specific DVD
     */
    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the serial number: ");
        int no = keyboard.nextInt();
        return no;
    }
    
    /**
     * returns a user-typed int.
     * arg is so it can say "old serial number" or "new serial number". leave blank to get default.
     * NOTE: remember a space after the arg, eg: "old "
     */
    private int inputSerialNumber(String arg)
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the " + arg + "serial number: ");
        int no = keyboard.nextInt();
        return no;
    }
    
    /**
     * Asks for and returns the price of a specific DVD
     */
    private double inputPriceOfPurchase()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What was the price of the DVD?: ");
        double price = keyboard.nextDouble();
        return price;
    }

    /**
     * Asks for a title and then prints the examples's (specific dvd's) serial number
     */
    private void findDVD()
    {     
        String title = inputTitle();

        ArrayList<Example> examples = dvdCtr.findDVD(title);
        System.out.println(examples.size() + "x \"" + title + "\" was found!");
        
        int counter = 1;
        for(Example example : examples)
        {
            System.out.println(counter + ") Serial Number: " + example.getSerialNumber());
        }   
    }
    /**
     * Adds a specific dvd (an example) to the Example container
     */
    private void createDVD()
    {
        //DVD:
        String title = inputTitle();
        String producer = inputProducer();
        String publisher = inputPublisher();
        
        Dvd dvd = new Dvd(title, producer, publisher);
        
        //Example:
        int serialNumber = inputSerialNumber("");
        String dateOfPurchase = inputDateOfPurchase();
        double priceOfPurchase = inputPriceOfPurchase();
        
        dvdCtr.createDVD(serialNumber, dateOfPurchase, priceOfPurchase, dvd);
        
    }
    /**
     * Prints information about a single example of a DVD
     */
    private void createCopy()
    {    
        Example example = getExample();
        if(example != null)
        {
            System.out.println("### DVD information ###");
            System.out.println("Title: " + example.getDvd().getTitle());
            System.out.println("Producer: " + example.getDvd().getProducer());
            System.out.println("Publish date: " + example.getDvd().getPublishDate());
            System.out.println("### Copy Information ###");
            System.out.println("Serial number: " + example.getSerialNumber());
            System.out.println("Purchased: " + example.getDateOfPurchase());
            System.out.println("Price: " + example.getPriceOfPurchase());
        }
        else
        {
            System.out.println("No copy exists with serial number " + example.getSerialNumber());
        }
    }
    
    /**
     * Asks for a serial number, and returns an Example object
     */
    public Example getExample()
    {
        int serialNumber = inputSerialNumber("");
        return dvdCtr.createCopy(serialNumber);
    }
    
    /**
     * removes a (specific) dvd from the example container
     */
    private void deleteDVD()
    {
        int serialNumber = inputSerialNumber("");
        
        dvdCtr.deleteDVD(serialNumber);
    }
    
    /**
     * Prints information about all DVDs
     */
    private void listAllDvds()
    {
        System.out.println("# of Examples *** Title *** Producer *** Year");
        System.out.println("#############################################");
        
        //The HashMap will contain a Dvd object and the number of examples of that dvd.
        HashMap<Dvd, Integer> allDvds = new HashMap<Dvd, Integer>();
        
        for(Example example : dvdCtr.listAllDvds())
        {
            Dvd exampleDvd = example.getDvd();
                //if the dvd already is listed
                if(allDvds.containsKey(exampleDvd))
                {
                    //then increase the integer value mapped to the dvd by 1.
                    int newValue = allDvds.get(exampleDvd) + 1;
                    allDvds.put(exampleDvd, newValue);
                }
                else
                {
                    //else add the dvd with mapped value of 1
                    allDvds.put(exampleDvd, 1);
                }
        }
        
        
        for(Dvd dvd : allDvds.keySet())
        {
            String title = dvd.getTitle();
            String producer = dvd.getProducer();
            String publishDate = dvd.getPublishDate();
            int noOfExamples =  allDvds.get(dvd);
            
            System.out.println(noOfExamples + " *** " + title + " *** " + producer + " *** " + publishDate);
        }

    }
    
    /**
     * Lists the DVDs currently NOT lend out
     */
    private void listCurrentDvds()
    {
        System.out.println("Examples *** Title *** Producer *** Year");
        System.out.println("#############################################");
        
        ArrayList<Dvd> allTitles = new ArrayList<Dvd>();
        for(Example example : dvdCtr.listAllDvds())
        {
            Dvd dvd = example.getDvd();
            if(allTitles.contains(dvd) == false)
            {
                allTitles.add(dvd);
            }
        }
        
        for(Dvd dvd : allTitles)
        {
            String title = dvd.getTitle();
            String producer = dvd.getProducer();
            String publishDate = dvd.getPublishDate();
            int noOfExamples =  dvdCtr.findDVD(title).size();
            
            System.out.println(noOfExamples + " *** " + title + " *** " + producer + " *** " + publishDate);
        }
 
    }
    
    /**
     * Asks the user to press RETURN to continue
     */
    private void pause()
    {
        System.out.println("Press RETURN to continue...");
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}






